// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('downloads', function () {

      Polymer({
            is: 'downloads-login',
            properties: {
                  user: {
                      type: Object,
                      value: {}
                  },
  
  
              },
              openDialog: function(){
                  console.log(11111111);
                  this.$.dialog.open();
              },
  
  
              login: function(){
  
                  var inputs = document.querySelectorAll('#login input'),
                      inputs_length = inputs.length,
                      is_valid = true;
  
                  for( var i = 0; i < inputs_length; i++ ) {
                      is_valid = inputs[i].validate();
                      inputs[i].focus();
                  }
  
                  if(is_valid) {
                      this.$.ajax.url = "http://127.0.0.1:8080/login";
                      this.$.ajax.params = this.user;
                      this.$.ajax.crossDomain= false,
                      this.$.ajax.options= {
                          xhrFields: {
                              withCredentials: true
                          }
                      };
                      this.$.ajax.generateRequest();
  
                  }
              },
              handleResponse: function(res) {
                  console.log(111);
                  var res = res.detail.response;
  
                  console.log(res);
                  console.log(222);
  
                  if(res) {
                      sessionStorage.setItem('id', res.id);
                      sessionStorage.setItem('email', res.email)
                      sessionStorage.setItem('token', res.token);
                      window.location = 'makesomenoice';
                  }
              }
            
      });


});
